<html>
<head><meta charset="utf-8"><title>custom operators · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html">custom operators</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="220023065"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220023065" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220023065">(Dec 15 2020 at 18:39)</a>:</h4>
<p>Hello! do we have any Pre-RFC / RFC about custom operators with custom precedence and fixity in Rust? I know it scares a bunch of people but it would be so practical… I’m currently writing an EDSL in Rust and it’s truly painful. For instance, I cannot override the <code>==</code> operator (<code>Eq</code> + <code>PartialEq</code>) because they return <code>bool</code> while I need <code>Expr&lt;bool&gt;</code>. The only sane way I know how to fix that is what we do in Haskell: define a custom operator, let’s say <code>.==</code>, that would have the correct type.</p>
<p>Is there any work regarding that matter already? If no, I’d be willing to start writing a Pre-RFC.</p>



<a name="220023709"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220023709" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220023709">(Dec 15 2020 at 18:45)</a>:</h4>
<p>Have you tried using a macro?</p>



<a name="220023893"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220023893" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220023893">(Dec 15 2020 at 18:47)</a>:</h4>
<p>If you want to use the same operators as rust does but give them new meaning, it shouldn't be so bad especially if you use a procedural macro</p>



<a name="220024894"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220024894" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220024894">(Dec 15 2020 at 18:55)</a>:</h4>
<p>I will say that I think operator overloading is somewhat discouraged by the community. Typically operators are only used when the meaning is very straight-forwarded (e.g., I might implement PartialEq for my Point type because equating two points is extremely straight forwarded). Many types in Rust don't support <code>=</code>, like raw pointers which have an <code>.eq</code> method instead.</p>



<a name="220024911"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220024911" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220024911">(Dec 15 2020 at 18:55)</a>:</h4>
<p>yes, I have some macros</p>



<a name="220024980"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220024980" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220024980">(Dec 15 2020 at 18:55)</a>:</h4>
<p>for an EDSL, using <code>.eq</code> is not really what I want</p>



<a name="220025059"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220025059" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220025059">(Dec 15 2020 at 18:56)</a>:</h4>
<p>right now I have something like:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">expr</span><span class="o">!</span><span class="p">(</span><span class="n">a</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">b</span><span class="p">);</span><span class="w"></span>
</code></pre></div>



<a name="220025067"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220025067" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220025067">(Dec 15 2020 at 18:56)</a>:</h4>
<p>but I’m not really satisfied with this</p>



<a name="220025095"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220025095" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220025095">(Dec 15 2020 at 18:56)</a>:</h4>
<p>I could also be using a proc-macro, but it’s not really the direction I want to take right now</p>



<a name="220025209"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220025209" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220025209">(Dec 15 2020 at 18:57)</a>:</h4>
<p>I was thinking more like an attribute on a full function containing many of your DSL ops</p>



<a name="220025292"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220025292" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220025292">(Dec 15 2020 at 18:58)</a>:</h4>
<p>yes, a proc-macro</p>



<a name="220025304"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220025304" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220025304">(Dec 15 2020 at 18:58)</a>:</h4>
<p>(i.e. a function attribute proc-macro)</p>



<a name="220025395"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220025395" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220025395">(Dec 15 2020 at 18:59)</a>:</h4>
<p>what do you want the interface to look like?</p>



<a name="220025583"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220025583" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220025583">(Dec 15 2020 at 19:00)</a>:</h4>
<p>the closest to Rust</p>



<a name="220025608"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220025608" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220025608">(Dec 15 2020 at 19:00)</a>:</h4>
<p>right now, besides a bunch of exception and thanks to type inference</p>



<a name="220025633"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220025633" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220025633">(Dec 15 2020 at 19:00)</a>:</h4>
<p>you don’t really see the difference between regular Rust expressions and the EDSL expressions</p>



<a name="220025657"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220025657" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220025657">(Dec 15 2020 at 19:00)</a>:</h4>
<p>it gets more complicated with the statements, though</p>



<a name="220025707"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220025707" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220025707">(Dec 15 2020 at 19:01)</a>:</h4>
<p>that seems like something a function attribute macro would do well with</p>



<a name="220025709"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220025709" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220025709">(Dec 15 2020 at 19:01)</a>:</h4>
<p>I have to have a <code>&amp;mut T</code> on an opaque <code>T</code>, accumulating the state</p>



<a name="220025737"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220025737" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220025737">(Dec 15 2020 at 19:01)</a>:</h4>
<p>probably, yes</p>



<a name="220025765"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220025765" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220025765">(Dec 15 2020 at 19:01)</a>:</h4>
<p>because you have all the usual structures of a function but they have new meaning</p>



<a name="220025791"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220025791" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220025791">(Dec 15 2020 at 19:01)</a>:</h4>
<p>I will investigate, I’ve written proc-macros already, it shouldn’t be too hard to do</p>



<a name="220025919"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220025919" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220025919">(Dec 15 2020 at 19:02)</a>:</h4>
<p>the problem with the proc-macro is that it makes it semi-magical</p>



<a name="220025955"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220025955" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220025955">(Dec 15 2020 at 19:02)</a>:</h4>
<p>and very hard to mix “normal” Rust expressions with the lifted ones into the EDSL</p>



<a name="220025959"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220025959" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220025959">(Dec 15 2020 at 19:02)</a>:</h4>
<p>I agree with Ryan though that this is the sort of thing folks will frown on</p>



<a name="220026028"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220026028" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220026028">(Dec 15 2020 at 19:03)</a>:</h4>
<p>because it <em>is</em> magical</p>



<a name="220026112"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220026112" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220026112">(Dec 15 2020 at 19:03)</a>:</h4>
<p>Another option is to delimit the macro to block scope</p>



<a name="220026120"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220026120" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220026120">(Dec 15 2020 at 19:03)</a>:</h4>
<p>for instance right now, if <code>x: Expr&lt;i32&gt;</code>, I can write:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">z</span>: <span class="kt">i32</span> <span class="o">=</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">z</span><span class="p">;</span><span class="w"></span>
</code></pre></div>



<a name="220026200"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220026200" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220026200">(Dec 15 2020 at 19:04)</a>:</h4>
<p><code>z</code> gets lifted into the EDSL without too much sugar</p>



<a name="220026212"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220026212" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220026212">(Dec 15 2020 at 19:04)</a>:</h4>
<p>that looks pretty magical to me</p>



<a name="220026237"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220026237" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220026237">(Dec 15 2020 at 19:04)</a>:</h4>
<p>if it's not actually adding something there</p>



<a name="220026249"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220026249" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220026249">(Dec 15 2020 at 19:04)</a>:</h4>
<p>while with a proc-macro… it would difficult to know whether <code>3</code> should be in the EDSL, or not</p>



<a name="220026264"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220026264" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220026264">(Dec 15 2020 at 19:04)</a>:</h4>
<p>yes, it’s building an EDSL</p>



<a name="220026298"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220026298" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220026298">(Dec 15 2020 at 19:05)</a>:</h4>
<p>that’s the whole purpose of an EDSL and why I would have preferred custom operators</p>



<a name="220026307"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220026307" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220026307">(Dec 15 2020 at 19:05)</a>:</h4>
<p>so that I could have, for instance, <code>.+</code></p>



<a name="220026315"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220026315" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220026315">(Dec 15 2020 at 19:05)</a>:</h4>
<p><code>.==</code>, etc.</p>



<a name="220026399"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220026399" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220026399">(Dec 15 2020 at 19:05)</a>:</h4>
<p>but you can do <code>let y = expr! { x + 3 };</code></p>



<a name="220026466"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220026466" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220026466">(Dec 15 2020 at 19:06)</a>:</h4>
<p>and maybe put more statements in the block to make it more interesting</p>



<a name="220026528"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220026528" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220026528">(Dec 15 2020 at 19:06)</a>:</h4>
<p>and now the code is clear that there is some magic happening and <code>x</code> was not actually added to <code>3</code></p>



<a name="220026717"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220026717" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220026717">(Dec 15 2020 at 19:08)</a>:</h4>
<p>that’s the current behavior</p>



<a name="220026742"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220026742" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220026742">(Dec 15 2020 at 19:08)</a>:</h4>
<p>because this doesn’t work:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"></span>
</code></pre></div>



<a name="220026748"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220026748" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220026748">(Dec 15 2020 at 19:08)</a>:</h4>
<p>it’s ambiguous</p>



<a name="220026774"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220026774" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220026774">(Dec 15 2020 at 19:08)</a>:</h4>
<p>so what's the problem then?</p>



<a name="220026775"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220026775" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220026775">(Dec 15 2020 at 19:08)</a>:</h4>
<p>it could be <code>Expr 1 + Expr 2</code> or <code>Expr (1 + 2)</code></p>



<a name="220026793"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220026793" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220026793">(Dec 15 2020 at 19:08)</a>:</h4>
<p>for this situation I have <code>lit!()</code></p>



<a name="220026812"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220026812" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220026812">(Dec 15 2020 at 19:09)</a>:</h4>
<p>so I guess I’ll have to use the same trick  for <code>==</code> etc.</p>



<a name="220026832"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220026832" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220026832">(Dec 15 2020 at 19:09)</a>:</h4>
<p>it would have been just better to have access to custom operators <span aria-label="stuck out tongue" class="emoji emoji-1f61b" role="img" title="stuck out tongue">:stuck_out_tongue:</span></p>



<a name="220026867"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220026867" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220026867">(Dec 15 2020 at 19:09)</a>:</h4>
<p>You can still have custom operators in your macro</p>



<a name="220026883"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220026883" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220026883">(Dec 15 2020 at 19:09)</a>:</h4>
<p>but you can also just steal the builtins</p>



<a name="220027066"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220027066" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220027066">(Dec 15 2020 at 19:11)</a>:</h4>
<p>just spitballing here, <code>expr! { x + eval(1 + 2) }</code> if you want to do evaluation/antiquoting in the middle of the expression (or maybe find a sigil that works there)</p>



<a name="220027119"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220027119" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220027119">(Dec 15 2020 at 19:11)</a>:</h4>
<p>I suppose you can look to the <code>quote!</code> macro for inspiration</p>



<a name="220027614"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220027614" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220027614">(Dec 15 2020 at 19:15)</a>:</h4>
<p>Of course none of this really answers your question. I think it would be great if rust supported custom operators a la haskell, although it might be hard to change the lexer in the middle of processing a crate</p>



<a name="220027833"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220027833" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220027833">(Dec 15 2020 at 19:17)</a>:</h4>
<p>it also makes it harder for future RFCs to invent new syntax if it can all potentially be used by users already</p>



<a name="220030877"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220030877" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220030877">(Dec 15 2020 at 19:37)</a>:</h4>
<p><span class="user-mention silent" data-user-id="282041">phaazon</span> <a href="#narrow/stream/122651-general/topic/custom.20operators/near/220023065">said</a>:</p>
<blockquote>
<p>For instance, I cannot override the <code>==</code> operator (<code>Eq</code> + <code>PartialEq</code>) because they return <code>bool</code> while I need <code>Expr&lt;bool&gt;</code>. The only sane way I know how to fix that is what we do in Haskell: define a custom operator, let’s say <code>.==</code>, that would have the correct type.</p>
</blockquote>
<p>How tied are you to it being a different operator?  If <code>PartialEq</code> <em>could</em> return a different type, would that be enough?</p>
<p>(An associated type that defaults to <code>bool</code> might be a more palatable change for people.)</p>



<a name="220032867"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220032867" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220032867">(Dec 15 2020 at 19:53)</a>:</h4>
<p><span class="user-mention" data-user-id="125270">@scottmcm</span> it would be enough, yes</p>



<a name="220032885"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220032885" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220032885">(Dec 15 2020 at 19:54)</a>:</h4>
<p>like what I already do with <code>+</code>, <code>-</code>, etc.</p>



<a name="220032935"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220032935" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220032935">(Dec 15 2020 at 19:54)</a>:</h4>
<p>but the problem is also the same for <code>&lt;</code>, <code>&gt;</code>, etc.</p>



<a name="220032955"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220032955" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220032955">(Dec 15 2020 at 19:54)</a>:</h4>
<p>(they return <code>Option&lt;Ordering&gt;</code>, basically)</p>



<a name="220033064"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/custom%20operators/near/220033064" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> phaazon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/custom.20operators.html#220033064">(Dec 15 2020 at 19:55)</a>:</h4>
<p><span class="user-mention" data-user-id="271719">@Mario Carneiro</span> yes I agree that it makes it harder to introduce new syntax without breaking change, but we have editions for this, don’t we? <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>